package com.czl.module_service.viewmodel

import androidx.databinding.ObservableField
import com.blankj.utilcode.util.GsonUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.tengyun.ListBean
import com.czl.base.data.bean.tengyun.PurchaseBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

class PurchaseMyApplyViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    var currentPage = 1
    var pageSize = 10
    val uc = UiChangeEvent()
    var tabIndex: Int? = 1
    var titleName = ObservableField("")

    class UiChangeEvent {
        val loadCompleteEvent: SingleLiveEvent<ListBean<PurchaseBean>> = SingleLiveEvent()
        val loadFinallyEvent: SingleLiveEvent<Unit?> = SingleLiveEvent()
    }

    val onLoadMoreListener: BindingCommand<Void> = BindingCommand(BindingAction {
        if (tabIndex == 1) {
            this.WXgetMyPurchaseApplicationList(titleName = titleName.get())
        } else {
            this.getPurchaseApplicationList(titleName = titleName.get())
        }
    })
    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        currentPage = 1
        if (tabIndex == 1) {
            this.WXgetMyPurchaseApplicationList()
        } else {
            this.getPurchaseApplicationList()
        }
    })

    val search: BindingCommand<Void> = BindingCommand(BindingAction {
        showLoading()
        currentPage = 1
        if (tabIndex == 1) {
            WXgetMyPurchaseApplicationList(titleName = titleName.get())
        } else {
            getPurchaseApplicationList(titleName = titleName.get())
        }
    })

    private fun WXgetMyPurchaseApplicationList(
        companyId: String? = model.getLoginCompanyId(),
        userId: String? = model.getLocalUserInfo()?.userId,
        titleName: String? = null
    ) {
        model.mTengYunHttpDataSource
            .WXgetMyPurchaseApplicationList(
                GsonUtils.toJson(
                    mapOf(
                        "pageNo" to currentPage,
                        "pageSize" to pageSize,
                        "userId" to userId,
                        "companyId" to companyId,
                        "titleName" to titleName
                    )
                )
            )
            .compose(RxThreadHelper.rxSchedulerHelper())
            .doFinally {
                uc.loadFinallyEvent.postValue(null)
            }
            .subscribe(object : ApiSubscriberHelper<BaseBean<ListBean<PurchaseBean>>>() {
                override fun onResult(t: BaseBean<ListBean<PurchaseBean>>) {
                    if (t.state == 0) {
                        currentPage++
                        uc.loadCompleteEvent.postValue(t.data)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }

            })
    }

    private fun getPurchaseApplicationList(
        companyId: String? = model.getLoginCompanyId(),
        userId: String? = model.getLocalUserInfo()?.userId,
        titleName: String? = null,
        statuses: ArrayList<Int> = arrayListOf<Int>(1, 2, 3, 4),
        taskType: Int = 1,     // 0是抄送，1是审批
        businessId: Int = 0
    ) {
        model.mTengYunHttpDataSource
            .getPurchaseApplicationList(
                GsonUtils.toJson(
                    mapOf(
                        "pageNo" to currentPage,
                        "pageSize" to pageSize,
                        "userId" to userId,
                        "companyId" to companyId,
                        "title" to titleName,
                        "statuses" to statuses,
                        "taskType" to taskType,
                        "businessId" to businessId
                    )
                )
            )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .doFinally {
                uc.loadFinallyEvent.postValue(null)
            }
            .subscribe(object : ApiSubscriberHelper<BaseBean<ListBean<PurchaseBean>>>() {
                override fun onResult(t: BaseBean<ListBean<PurchaseBean>>) {
                    if (t.state == 0) {
                        currentPage++
                        uc.loadCompleteEvent.postValue(t.data)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }

            })
    }
}